package com.app.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.app.biz.contents.ContentsUtil;
import com.app.biz.domain.BizProduceSale;
import com.app.biz.domain.BizSocialElectricityConsumption;
import com.app.biz.excel.entity.BizPmiExcelEntity;
import com.app.biz.excel.entity.BizProduceSaleExcelEntity;
import com.app.biz.mapper.BizPmiFormalMapper;
import com.app.common.utils.DateUtils;
import com.app.common.utils.DictUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizPmiMapper;
import com.app.biz.domain.BizPmi;
import com.app.biz.service.IBizPmiService;
import org.springframework.util.StringUtils;

/**
 * 山东制造业PMIService业务层处理
 * 
 * @author ZhouS
 * @date 2024-01-02
 */
@Service
public class BizPmiServiceImpl implements IBizPmiService 
{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BizPmiMapper bizPmiMapper;

    @Autowired
    private BizPmiFormalMapper bizPmiFormalMapper;

    /**
     * 查询山东制造业PMI
     * 
     * @param baseId 山东制造业PMI主键
     * @return 山东制造业PMI
     */
    @Override
    public BizPmi selectBizPmiByBaseId(String baseId)
    {
        return bizPmiMapper.selectBizPmiByBaseId(baseId);
    }

    /**
     * 查询山东制造业PMI列表
     * 
     * @param bizPmi 山东制造业PMI
     * @return 山东制造业PMI
     */
    @Override
    public List<BizPmi> selectBizPmiList(BizPmi bizPmi)
    {
        return bizPmiMapper.selectBizPmiList(bizPmi);
    }

    /**
     * 新增山东制造业PMI
     * 
     * @param bizPmi 山东制造业PMI
     * @return 结果
     */
    @Override
    public int insertBizPmi(BizPmi bizPmi)
    {
        bizPmi.setBaseId(IdUtil.simpleUUID());
        bizPmi.setBaseCreateTime(new Date());
        DateTime datesTimes= DateUtil.parse(bizPmi.getBaseMonth(),"yyyy年MM月");
        bizPmi.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizPmiMapper.insertBizPmi(bizPmi);
    }

    /**
     * 修改山东制造业PMI
     * 
     * @param bizPmi 山东制造业PMI
     * @return 结果
     */
    @Override
    public int updateBizPmi(BizPmi bizPmi)
    {
        DateTime datesTimes= DateUtil.parse(bizPmi.getBaseMonth(),"yyyy年MM月");
        bizPmi.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizPmiMapper.updateBizPmi(bizPmi);
    }

    /**
     * 批量删除山东制造业PMI
     * 
     * @param baseIds 需要删除的山东制造业PMI主键
     * @return 结果
     */
    @Override
    public int deleteBizPmiByBaseIds(String[] baseIds)
    {
        return bizPmiMapper.deleteBizPmiByBaseIds(baseIds);
    }

    /**
     * 删除山东制造业PMI信息
     * 
     * @param baseId 山东制造业PMI主键
     * @return 结果
     */
    @Override
    public int deleteBizPmiByBaseId(String baseId)
    {
        return bizPmiMapper.deleteBizPmiByBaseId(baseId);
    }

    /**
     * <p>
     * 查询APP左上角的日期数据
     * </p>
     *
     * @return 日期的集合
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizPmiMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            List<String> collect = uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
            collect = collect.stream().map(item -> {
                String[] split = item.split("-");
                return split[0] + "年" + split[1] + "月";
            }).collect(Collectors.toList());
            return collect;
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     * 清空所有的山东制造业PMI数据
     * </p>
     *
     * @return
     */
    @Override
    public void clearAllTableData() {
        bizPmiMapper.clearAllTableData();
    }


    /**
     * @return
     */
    @Override
    public List<BizPmi> selectBizPmiListNoParams() {
        return bizPmiMapper.selectBizPmiListNoParams();
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try {
            List<BizPmi> result = bizPmiMapper.selectBizPmiAllData();

            int count = 0;
            List<BizPmi> list = new ArrayList<>();
            for (BizPmi item : result) {
                list.add(item);
                count++;
                if (count >= 100) {
                    bizPmiFormalMapper.batchInsertBizPmiFormal(list);
                    list.clear();
                }
            }

            if (!list.isEmpty()) {
                bizPmiFormalMapper.batchInsertBizPmiFormal(list);
                list.clear();
            }
            return true;
        } catch (Exception e) {
            logger.error("未知错误:{}", e.getMessage());
            return false;
        }
    }

    /**
     * @param absoluteExcelPath
     * @param yearMonth
     */
    @Override
    public void importExcel(String absoluteExcelPath, String yearMonth) {
        List<BizPmiExcelEntity> excelDataArgs = EasyExcel.read(absoluteExcelPath).head(BizPmiExcelEntity.class).headRowNumber(1).sheet().doReadSync();
        if(excelDataArgs.isEmpty()){
            logger.warn("没有找到PMI数据");
        }else{
            List<BizPmi> list = new ArrayList<>();
            for(BizPmiExcelEntity i : excelDataArgs){
                BizPmi temp = new BizPmi();
                temp.setBaseId(IdUtil.simpleUUID());

                temp.setBaseMonth(yearMonth);
                temp.setMonthNum(i.getMonthNum());
                temp.setAddOrDe(i.getAddOrDe());
                temp.setIndustryPmi(DictUtils.getDictValue("industry_pmi",i.getIndustryPmi()));
                temp.setIntervals(i.getIntervals());
                temp.setTrend(i.getTrend());
                temp.setBaseCreateTime(DateUtils.getNowDate());
                list.add(temp);

                if(list.size() > 100){
                    bizPmiMapper.batchBizPmi(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizPmiMapper.batchBizPmi(list);
                list.clear();
            }
        }
    }
}
